package com.lz.comm.widget;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.lz.library.extension.UtilsExtenKt;

/**
 * Created by you on 2016/8/5.
 */

public class RecyclerViewDivider extends RecyclerView.ItemDecoration {
    /**
     * 默认的分割线颜色
     */
    private static final int DEF_COLOR = Color.parseColor("#F6F7FB");
    /**
     * 分割线宽度
     */
    private final int dividerWidth;
    /**
     * paint
     */
    private final Paint paint;

    public RecyclerViewDivider() {
        this(UtilsExtenKt.dp2px(1f), DEF_COLOR);
    }

    public static RecyclerViewDivider publicDivider() {
        return new RecyclerViewDivider();
    }

    public RecyclerViewDivider(int dividerWidth, @ColorInt int dividerColor) {
        this.dividerWidth = dividerWidth;
        this.paint = new Paint();
        paint.setColor(dividerColor);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.LayoutManager lm = parent.getLayoutManager();
        if (lm instanceof LinearLayoutManager) {
            LinearLayoutManager llm = (LinearLayoutManager) lm;
            if (llm.getOrientation() == LinearLayoutManager.HORIZONTAL) {
                outRect.set(0, 0, dividerWidth, 0);
            } else {
                outRect.set(0, 0, 0, dividerWidth);
            }
        } else if (lm instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager glm = (StaggeredGridLayoutManager) lm;
            if (glm.getOrientation() == StaggeredGridLayoutManager.HORIZONTAL) {
                outRect.set(0, 0, dividerWidth, 0);
            } else {
                outRect.set(0, 0, 0, dividerWidth);
            }
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        RecyclerView.LayoutManager lm = parent.getLayoutManager();
        if (lm instanceof LinearLayoutManager) {
            LinearLayoutManager llm = (LinearLayoutManager) lm;
            int firstPostion = llm.findFirstVisibleItemPosition();
            int lastPosition = llm.findLastVisibleItemPosition();
            for (int i = firstPostion; i<= lastPosition; i++) {
                RecyclerView.ViewHolder vh = parent.findViewHolderForAdapterPosition(i);
                if (vh == null) continue;
                View child = vh.itemView;
                Rect rect = new Rect();
                if(llm.getOrientation() == LinearLayoutManager.VERTICAL) {
                    rect.set(child.getLeft(), child.getBottom(), child.getRight(), child.getBottom() + dividerWidth);
                } else {
                    rect.set(child.getRight(), child.getTop(), child.getRight() + dividerWidth, child.getBottom());
                }
                c.drawRect(rect, paint);
            }
        } else if (lm instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager glm = (StaggeredGridLayoutManager) lm;
            if (glm.getOrientation() == StaggeredGridLayoutManager.HORIZONTAL) {

            } else {

            }
        }
    }

}
